package com.zjlanyun.erp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.yolanda.nohttp.rest.Response;
import com.zjlanyun.erp.config.AppConfig;
import com.zjlanyun.erp.network.WebApi;
import com.zjlanyun.erp.nohttp.HttpListener;
import com.zjlanyun.erp.nohttp.HttpRequest;
import com.zjlanyun.erp.nohttp.SimpleHttpListener;
import com.zjlanyun.erp.utils.Toastor;
import com.zjlanyun.erp.view.BaseSwipeBackActivity;

import org.json.JSONArray;
import org.json.JSONObject;

public class ConnectSetup extends BaseSwipeBackActivity {

    private Button btn_save;
    private EditText et_server_url;
    private String serverUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connect_setup);
        initBaseView();
        initView();
    }

    private void initView() {
        btn_save = (Button) findViewById(R.id.btn_save);
        et_server_url = (EditText) findViewById(R.id.et_server_url);

        if (!AppConfig.getInstance().getString("serverUrl", "").equals("")) {
            if (AppConfig.getInstance().getBoolean("isDemo", true)) {
                et_server_url.setText(AppConfig.getInstance().getString("serverUrlDemo", ""));
            } else {
                et_server_url.setText(AppConfig.getInstance().getString("serverUrl", ""));
            }
        }

        title.setText("连接设置");

        //保存
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                serverUrl = et_server_url.getText().toString();
                if (serverUrl.equals("")) {
                    Toastor.getInstance().showShortToast("请输入业务系统地址");
                } else {
                    // 判断当前是否为演示
                    if (AppConfig.getInstance().getBoolean("isDemo", true)) {
                        AppConfig.getInstance().putString("serverUrlDemo", serverUrl);
                        scrollToFinishActivity();
                    } else {
                        HttpRequest client = new HttpRequest(mContext, "http://" + serverUrl + "/user.aspx");
                        client.getRequest().add("action", "config");
                        client.send(new SimpleHttpListener<JSONObject>() {
                            @Override
                            public void onSucceed(int what, Response<JSONObject> response) {
                                JSONArray module = response.get().optJSONObject("data").optJSONArray("module");
                                AppConfig.getInstance().putString("module", module.toString());
                                AppConfig.getInstance().putString("serverUrl", serverUrl);
                                AppConfig.getInstance().putString("customer", response.get().optJSONObject("data").optString("customer"));
                                AppConfig.getInstance().putString("device_bind_url", response.get().optJSONObject("data").optString("device_bind_url"));
                                AppConfig.getInstance().putString("device_unbind_url", response.get().optJSONObject("data").optString("device_unbind_url"));
                                AppConfig.getInstance().putString("device_logout_url", response.get().optJSONObject("data").optString("device_logout_url"));
                                // 判断用户是否已经登录
                                if (AppConfig.getInstance().getBoolean("isLogin", false)) {
                                    scrollToFinishActivity();
                                } else {
                                    Intent intent = new Intent(ConnectSetup.this, Login.class);
                                    startActivity(intent);
                                    finish();
                                }
                            }
                        });
                    }
                }
            }
        });
    }
}
